package com.platform.acl.controller;


import com.platform.acl.entity.User;
import com.platform.acl.service.UserService;
import com.platform.common_utils.CheckCodeUtil;
import com.platform.common_utils.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author CZF
 * @author LJH
 * @since 2022-02-23
 */
@Api(tags = "登录注册")
@RestController
@RequestMapping("/acl/user")
@CrossOrigin
public class UserController {

    @Autowired
    private UserService userService;

    @ApiOperation("验证验证码")
    @GetMapping("/checkCode/{checkCode}")
    public R checkCode(@PathVariable(value = "checkCode") String checkCode, HttpSession session){
        // 检测验证码是否一致
        String checkCodeGen = (String) session.getAttribute("checkCodeGen");
        if (checkCode.equalsIgnoreCase(checkCodeGen)) {
            // 验证码正确
            return R.ok();
        }
        return R.error().message("验证码错误");
    }

    @ApiOperation("获取图片验证码")
    @GetMapping("/getCode")
    public void getCode(HttpServletResponse response, HttpSession session) throws IOException {
        // 获取验证码
        String checkCode = CheckCodeUtil.outputVerifyImage(100, 50, response.getOutputStream(), 4);
        System.out.println(checkCode);
        // 将验证码存入到Session中
        session.setAttribute("checkCodeGen", checkCode);
    }

    //注册新用户
    @ApiOperation("注册新用户")
    @PostMapping("/addUser")
    public R addUser(@RequestBody User user){
        return userService.addUser(user);
    }

    //退出登录
    @ApiOperation("退出登录")
    @PostMapping("logout")
    public R logout(){
        return R.ok();
    }

}

